package GUI; //done

import java.awt.SystemColor;
import java.awt.HeadlessException;
import java.awt.Rectangle;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import Client.ClientAbstractWindow;
import Client.ConnectionToServer;
import Common.Settings;

/**
 * This Class controls Market Manager Window.
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil
 */
public class MarketManagerWindow extends ClientAbstractWindow {
	/**
	 * The serial Version UID.
	 */
	private static final long serialVersionUID = 6120687338269912784L;
	
	/** The Deal Sale Button. */
	private JButton DealSaleButton = null;
	
	/** The Customer Report Button. */
	private JButton customerReportButton = null;
	
	/** The Response Report Button. */
	private JButton responseReportButton = null;
	
	/** The Catalog Management Button. */
	private JButton CatalogManagementButton = null;
	
	/** The Add Address Zone Button. */
	private JButton addAddressZoneButton = null;
	
	/** The Update Address Zone Button. */
	private JButton updateAddressZoneButton = null;	
	
	/** The catalog management Form. */
	private CatalogManagementWindow catalogmanagementForm = null;
	
	/** Add Address Zone JFrame. */
	private JFrame addAddressZoneJFrame = null;
	
	/** Update Address Zone JFrame. */
	private JFrame updateAddressZoneJFrame = null;
	
	/** the Report Form window. */
	private ReportWindow reportWindow = null;
	
	/** Labels For Photos and backgrounds. */
	private JLabel MarketManagerIcon;
	private JLabel BackgroundJLabel;
	
	/**
	 * The default constructor
	 */
	public MarketManagerWindow() {
		super();
		setBackground(SystemColor.activeCaption);
	   
		initialize();	
	}
	
	/**
	 * This function initializes MarketManagerWindow
	 */
	private void initialize() {
		this.setSize(600, 454);
		this.setLayout(null);
		getUsernameLabel().setText("Welcome "+Settings.Logged_Username);
		this.add(getDealSaleButton(),null);
		this.add(getCustomerReportButton(),null);
		this.add(getResponseReportButton(),null);
		this.add(getCatalogManagementButton(),null);
		this.add(getAddAddressZoneButton() ,null);	
		this.add(getUpdateAddressZoneButton(),null);
		add(getMarketManagerIcon());
		add(getBackground1());
	}
	
	/**
	 * This function initializes DealSaleButton	
	 */
	private JButton getDealSaleButton() {
		if (DealSaleButton == null) 
		{
			DealSaleButton = new JButton();
			DealSaleButton.setBounds(new Rectangle(31, 117, 172, 29));
			DealSaleButton.setText("initialize Deal Sale");
			DealSaleButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{			
					try{						
						TemplateSearchWindow templatesearch = new TemplateSearchWindow("DealSale");
						templatesearch.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE);
						templatesearch.setBounds(Settings.Screen_x, Settings.Screen_y, 520, 500 ); // set frame size
						templatesearch.setVisible( true ); // display frame
					}catch(HeadlessException headlessexception){
						JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
					}	
				}
			});
		}
		return DealSaleButton;
	}
	
	/**
	 * This function initializes customerReportButton
	 */
	private JButton getCustomerReportButton() {
		if (customerReportButton == null) {
			customerReportButton = new JButton();
			customerReportButton.setBounds(new Rectangle(31, 186, 172, 29));
			customerReportButton.setText("Customers Report");
			customerReportButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {	
					try{
						ConnectionToServer connectionToServer = new ConnectionToServer();
						String msg = (String)connectionToServer.customerReport();
						if(msg != null){	
							reportWindow = new ReportWindow();
							JFrame frame = new JFrame("Reports"); 
							frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
							//Add content to the window. 
							frame.getContentPane().add(reportWindow); 
							//Display the window. 
							frame.pack();
							frame.setSize(350, 450);
							reportWindow.setText(msg);
							frame.setVisible(true);													
						}	
						else
							JOptionPane.showMessageDialog(null, "CAMPAIGN NOT EXIST IN DATA BASE","",JOptionPane.WARNING_MESSAGE);
					}catch(Exception exc){
						JOptionPane.showMessageDialog(null, "ERROR","",JOptionPane.ERROR_MESSAGE);
					}
				}
			});
		}
		return customerReportButton;
	}
	
	
	/**
	 * This function initializes responseReportButton
	 */
	private JButton getResponseReportButton() {
		if (responseReportButton == null) {
			responseReportButton = new JButton();
			responseReportButton.setBounds(new Rectangle(31, 256, 172, 29));
			responseReportButton.setText("Response Report");
			responseReportButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {			
					DealSaleSearchWindow dealsalesearch = new DealSaleSearchWindow();
					dealsalesearch.setBounds(Settings.Screen_x, Settings.Screen_y, 300, 300);
					dealsalesearch.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
					dealsalesearch.setVisible(true);
				}
			});
		}
		return responseReportButton;
	}
	
	/**
	 * This function initializes CatalogManagementButton
	 */
	private JButton getCatalogManagementButton() {
		if (CatalogManagementButton == null) {
			CatalogManagementButton = new JButton();
			CatalogManagementButton.setBounds(new Rectangle(248, 117, 172, 29));
			CatalogManagementButton.setText("Catalog Management");
			CatalogManagementButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {	
					catalogmanagementForm = new CatalogManagementWindow();
					catalogmanagementForm.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
					catalogmanagementForm.setVisible(true);
				}
					
			});
		}
		return CatalogManagementButton;
	}
	
	/**
	 * This function initializes addAddressZoneButton
	 */
	private JButton getAddAddressZoneButton() {
		if (addAddressZoneButton == null) {
			addAddressZoneButton = new JButton();
			addAddressZoneButton.setBounds(new Rectangle(248, 186, 172, 29));
			addAddressZoneButton.setText("Add address zone");
			addAddressZoneButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {	
					AddressZoneWindow addressZoneWindow = null;
					try{
						addAddressZoneJFrame = new JFrame();
						addressZoneWindow = new AddressZoneWindow();						
					}
					catch(HeadlessException headlessexception){
						JOptionPane.showMessageDialog(null, "Fatal Error!");
						return;
					}
					addAddressZoneJFrame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
					addAddressZoneJFrame.setBounds(Settings.Screen_x, Settings.Screen_y, 300, 220 ); // set frame size
					addAddressZoneJFrame.setTitle("Add address zone");
					addressZoneWindow.addSaveButton();
					addAddressZoneJFrame.setResizable(false);	
					addressZoneWindow.getCancelButton().addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							
						}
					});		
					
					JLabel backgroundLabel = new JLabel("");
					backgroundLabel.setIcon(new ImageIcon(AddCustomerInterestWindow.class.getResource("/Pictures/bg.png")));
					backgroundLabel.setBounds(0, 0, 300, 330);
					addressZoneWindow.add(backgroundLabel);
					
					addAddressZoneJFrame.setContentPane(addressZoneWindow);
					addAddressZoneJFrame.setVisible( true ); // display frame
				}
			});
		}
		return addAddressZoneButton;
	}	
	
	/**
	 * This function initializes updateAddressZoneButton
	 */
	private JButton getUpdateAddressZoneButton() {
		if (updateAddressZoneButton == null) {
			updateAddressZoneButton = new JButton();
			updateAddressZoneButton.setBounds(new Rectangle(248, 256, 172, 29));
			updateAddressZoneButton.setText("Update address zone");
			updateAddressZoneButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {			
					AddressZoneWindow addressZoneWindow = null;
					try{
						updateAddressZoneJFrame = new JFrame();
						addressZoneWindow = new AddressZoneWindow();						
					}
					catch(HeadlessException headlessexception){
						JOptionPane.showMessageDialog(null, "Fatal Error!");
						return;
					}
					updateAddressZoneJFrame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
					updateAddressZoneJFrame.setBounds(Settings.Screen_x, Settings.Screen_y, 300, 220 ); // set frame size
					updateAddressZoneJFrame.setTitle("Update address zone");
					addressZoneWindow.addUpdateButton();
					addressZoneWindow.getCancelButton().addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
												
						}
					});		
					
					JLabel backgroundLabel = new JLabel("");
					backgroundLabel.setIcon(new ImageIcon(AddCustomerInterestWindow.class.getResource("/Pictures/bg.png")));
					backgroundLabel.setBounds(0, 0, 300, 330);
					addressZoneWindow.add(backgroundLabel);
					
					updateAddressZoneJFrame.setContentPane(addressZoneWindow);
					updateAddressZoneJFrame.setVisible( true ); // display frame
				}
			});			
		}
		return updateAddressZoneButton;
	}	
	private JLabel getMarketManagerIcon() {
		if (MarketManagerIcon == null) {
			MarketManagerIcon = new JLabel("");
			MarketManagerIcon.setIcon(new ImageIcon(LoginMenu.class.getResource("/Pictures/Manager-icon.png")));
			MarketManagerIcon.setBounds(55, 11, 300, 72);
		}
		return MarketManagerIcon;
	}
	private JLabel getBackground1() {
		if (BackgroundJLabel == null) {
			BackgroundJLabel = new JLabel("");
			BackgroundJLabel.setIcon(new ImageIcon(LoginMenu.class.getResource("/Pictures/bg.png")));
			BackgroundJLabel.setBounds(0, -192, 600, 646);
		}
		return BackgroundJLabel;
	}
}
